package surface;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class AverageCalcPanel extends JPanel {
	
	private JButton addButton, deleteButton;
	private PictureBox originalPict, averagesPict;
	private JSlider originalSlider;
	private JLabel fromL, toL;
	private JTextField from, to;
	private JList segmentList;
	
	
	public AverageCalcPanel(ActionListener al, ChangeListener cl){

		generateSurface();
		addButton.addActionListener(al);
		originalSlider.addChangeListener(cl);
		
	}

	public void generateSurface(){
		addButton = new JButton();
		addButton.setFont(new Font(addButton.getFont().toString(), 0, 14));
		addButton.setText("Add");
		
		originalPict = new PictureBox(550, 300);
		originalPict.setBackground(Color.GRAY);
		
		averagesPict = new PictureBox(550, 300);
		averagesPict.setBackground(Color.GRAY);
		averagesPict.setMulti(true);
		
		originalSlider = new JSlider();
		originalSlider.setMinimum(0);
		originalSlider.setMaximum(1000);
		originalSlider.setValue(0);
		originalSlider.setMajorTickSpacing(1);
		
		from = new JTextField();
		fromL = new JLabel();
		fromL.setText("From: ");
		to = new JTextField();
		toL = new JLabel();
		toL.setText("To: ");
		
		deleteButton = new JButton();
		deleteButton.setFont(new Font(addButton.getFont().toString(), 0, 14));
		deleteButton.setText("Delete");
		
		segmentList = new JList();
		segmentList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				if(((JList)arg0.getSource()).getSelectedIndices().length>0) {
					deleteButton.setEnabled(true);
				} else {
					deleteButton.setEnabled(false);
				}
			}
		});
		
		this.setLayout(new XYLayout());
		this.add(originalPict, new XYConstraints(10, 50, originalPict.getWidth(),
				originalPict.getHeight()));
		this.add(originalSlider, new XYConstraints(10, 60+originalPict.getHeight(), originalPict.getWidth(), 30));
		
		this.add(averagesPict, new XYConstraints(20+originalPict.getWidth(), 50, averagesPict.getWidth(),
				averagesPict.getHeight()));
		this.add(addButton, new XYConstraints(350, 400, 120, 20));
		this.add(deleteButton, new XYConstraints(350, 430, 120, 20));
		this.add(fromL, new XYConstraints(20, originalPict.getHeight()+100, 30, 20));
		this.add(from, new XYConstraints(55, originalPict.getHeight()+100, 120, 20));
		this.add(toL, new XYConstraints(180, originalPict.getHeight()+100, 30, 20));
		this.add(to, new XYConstraints(215, originalPict.getHeight()+100, 120, 20));
		this.add(segmentList, new XYConstraints(100, originalPict.getHeight()+150, 220, 150));
		
	}
}
